package controller;

import entity.WorkOrder;
import Service.WorkOrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

@RestController
@RequestMapping("/api/work-order")
public class WorkOrderController {

    @Resource
    private WorkOrderService workOrderService;

    // 创建工单
    @PostMapping("/create")
    public ResponseEntity<String> addWorkOrder(@RequestBody WorkOrder workOrder) {
        try {
            workOrderService.addWorkOrder(workOrder);
            return ResponseEntity.ok("工单创建成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("系统异常：" + e.getMessage());
        }
    }

    // 工单回单
    @PutMapping("/reply")
    public ResponseEntity<String> replyWorkOrder(@RequestBody WorkOrder workOrder) {
        try {
            workOrderService.replyWorkOrder(workOrder);
            return ResponseEntity.ok("回单操作成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("系统异常：" + e.getMessage());
        }
    }

    // 工单退单
    @PutMapping("/cancel")
    public ResponseEntity<String> cancelWorkOrder(@RequestBody WorkOrder workOrder) {
        try {
            workOrderService.cancelWorkOrder(workOrder);
            return ResponseEntity.ok("退单操作成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("系统异常：" + e.getMessage());
        }
    }

    // 区域内工单查询
    @PostMapping("/region/query")
    public ResponseEntity<?> selectWorkOrderByRegion(@RequestBody WorkOrder workOrder) {
        try {
            return ResponseEntity.ok("查询成功，实际需补充返回查询到的工单数据");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("系统异常：" + e.getMessage());
        }
    }
}